package pannelli.sub;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;
import it.jack.Main;
import oggetti.Cliente;
import oggetti.Magazzino;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import pannelli.main.PannelloMagazzino;

public class SubPannelloAccessoriInMagazzino extends javax.swing.JPanel {

    private static final long serialVersionUID = 3638873279821330809L;
    private JScrollPane jScrollPane1;
    private DefaultTableModel model;
    private boolean flag;
    private JTable tabellaAccessori;
    private int row;
    private static SubPannelloAccessoriInMagazzino instance;

    private SubPannelloAccessoriInMagazzino() {
        super();
        initGUI();
        flag = false;
        initTable();
    }
    public static SubPannelloAccessoriInMagazzino getInstance(){
       if(instance==null)
           instance = new SubPannelloAccessoriInMagazzino();
       return instance;
    }

    public boolean removeAccessorio() {
        Magazzino m = getAccessorioFromRow(getCurrentRow());
        System.out.println("ID = "+m.getId());
        model.removeRow(getCurrentRow());
        new DB().cancellaAccessorio(m.getId());
        return true;
    }

    public int getCurrentRow() {
        return row;
    }

    public int getMaxRow() {
        return tabellaAccessori.getRowCount();
    }

    public Magazzino getAccessorioFromRow(int row) {
        int id = Integer.parseInt(tabellaAccessori.getValueAt(row, 0).toString());
        String marca = (String) tabellaAccessori.getValueAt(row, 1);
        String modello = (String) tabellaAccessori.getValueAt(row, 2);
        String imei = (String) tabellaAccessori.getValueAt(row, 3);
        String seriale = (String) tabellaAccessori.getValueAt(row, 4);
        String prezzoAcq = (String) tabellaAccessori.getValueAt(row, 5);
        String prezzoRiv = (String) tabellaAccessori.getValueAt(row, 6);
        String condizione_ = (String) tabellaAccessori.getValueAt(row, 7);
        Magazzino c = new Magazzino(id, marca, modello, imei, seriale,prezzoAcq,prezzoRiv,condizione_);
        return c;
    }

    private void initGUI() {
        try {
            AnchorLayout thisLayout = new AnchorLayout();
            this.setLayout(thisLayout);
            //this.setPreferredSize(new java.awt.Dimension(649, 420));
        } catch (Exception e) {
            e.printStackTrace();
        }
        model = new DefaultTableModel();
        tabellaAccessori = new JTable(model);
        //tabellaAccessori.setPreferredSize(new java.awt.Dimension(822, 298));
        jScrollPane1 = new JScrollPane();
        this.add(jScrollPane1, new AnchorConstraint(1, 1000, 1001, 0, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(799, 264));
        jScrollPane1.setViewportView(tabellaAccessori);

        String[] columnNames = {"id", "Marca", "Modello", "Imei", "Seriale","Acquisto","Rivendita","Condizione"};
        for (int i = 0; i < columnNames.length; i++) {
            model.addColumn(columnNames[i]);
        }
        if (PannelloMagazzino.getIstance() != null) {
            tabellaAccessori.addMouseListener(new MouseListener() {

                @Override
                public void mouseReleased(MouseEvent e) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void mousePressed(MouseEvent e) {
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void mouseClicked(MouseEvent arg0) {
                    if (Main.getInstance().getUser().getUsername().equals("admin")) {
                       // if (!flag) {
                          //  flag = true;
                            PannelloMagazzino.getIstance().AbilitaRimozione(false);
                            row = tabellaAccessori.getSelectedRow();
                            if (row >= 0) {
                                PannelloMagazzino.getIstance().AbilitaRimozione(true);
                            } else {
                                PannelloMagazzino.getIstance().AbilitaRimozione(false);
                            }
                        //}
                    }
                }
            });
        }
        tabellaAccessori.getColumnModel().getColumn(0).setPreferredWidth(30);
        //tabellaAccessori.setPreferredSize(new java.awt.Dimension(631, 417));

    }

    public void initTable() {
        int a = model.getRowCount();
        for (int i = 0; i < a; i++) {
            model.removeRow(0);
        }

        ArrayList<Magazzino> c = new DB().getAccessori();
        if (c != null) {
            for (Magazzino tmp : c) {
                model.addRow(tmp.toArray());
            }
        }
    }
    public void initTable(ArrayList<Magazzino> c) {
        int a = model.getRowCount();
        for (int i = 0; i < a; i++) {
            model.removeRow(0);
        }

        if (c != null) {
            for (Magazzino tmp : c) {
                model.addRow(tmp.toArray());
            }
        }
    }
}
